package com.cdut.service.impl;

import com.cdut.domain.User;
import com.cdut.mapper.UserMapper;
import com.cdut.service.UserService;
import com.cdut.utils.Constants;
import com.cdut.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<User> queryUsers() {
        List<User> list = null;
        boolean flag = redisUtil.hasKey(Constants.User_List);
        if(flag){
            System.out.println("从缓存中取");
            list = (List<User>)(List)redisUtil.lGet(Constants.User_List,0,-1);
        }else{
            System.out.println("从数据库中取");
            list = userMapper.findAll();
            redisUtil.lSet(Constants.User_List,list);
        }
        return list;
    }

    @Override
    public User findById(int id) {
        return userMapper.findByid(id);
    }
}
